package LL1checker;

import java.util.ArrayList;

public class BNF {
	private ArrayList<Expression> expressions;
	private static BNF uniqueInstance;
	
	public BNF(){
		expressions = new ArrayList<Expression>();
	}
	
	public static BNF getInstance(){
		if (uniqueInstance == null){
			uniqueInstance = new BNF();
		}
		return uniqueInstance;
	}
	
	public void setStarters(){
		for (Expression ex : expressions){
			//TODO:
			//ex.defineStarters();
		}
	}
	
	public void setFollowers(){
		for (Expression ex : expressions){
			//TODO:
			// ex.defineFollowers
		}
	}
	
	public void addTerminal(Terminal terminal){
		
	}
	
	public void addNonTerminal(NonTerminal nTerm){
		
	}
	

	public ArrayList<Expression> getExpressions(){
		return expressions;
	}
	
	public void checkFirstRule(){
		for (Expression ex : expressions){
			ArrayList<Term> tmpStarters = ex.getStarters();
			for (Term t : tmpStarters){
				if (!tmpStarters.isEmpty()){
					
				}
				
			}
			
		}		
	}
	
	public Expression getExpressionThroughTerm(Term searchTerm){
		//TODO:
		NonTerminal nt = (NonTerminal) searchTerm;		
		return null;
	}
	
	private ArrayList<Term> getStartersFromNonterm(NonTerminal nTerm, ArrayList<Term> valuesPassed){
		//TODO: MOD
        ArrayList<Term> starters = new ArrayList<Term>();        
        valuesPassed.add(nTerm);
        
        for(Term term : nTerm.getStarters()){
                if(term instanceof Terminal || term instanceof Empty){
                        starters.add(term);
                }
                if(term instanceof NonTerminal && !(valuesPassed.contains(term))){                        
                        starters.addAll(getStartersFromNonterm((NonTerminal) term, valuesPassed));
                }
        }
        return starters;
	}
	
	
	
	public void checkSecondRule(){
		
	}
	
	public void checkThirdRule(){
		
	}

}
